package db

import (
	"aicloud/monitor-apiserver/pkg/model"
	"gorm.io/gorm"

	"aicloud/monitor-apiserver/pkg/util"
	_const "aicloud/monitor-apiserver/pkg/util/const"
)

var (
	RegisterFunc = map[string]func(url string) Database{}
	DbClient     Database
)

type Database interface {
	GetDataCenterCount() (int, error)
	GetAllSupplierCount(idcName string) (int, error)
	GetAllSuppliers() ([]model.SupplierInfo, error)
}

type DB struct {
	*gorm.DB
}

func NewDatabaseClient() Database {
	dbType := util.GetEnvOrDefault(_const.DB_TYPE_ENV, "postgres")
	url := util.GetEnvOrDefault(_const.DB_ADDRESS_ENV, "host=localhost port=5432 user=postgres dbname=aicloud password=postgres")
	initFunc := RegisterFunc[dbType]
	if initFunc == nil {
		util.GetLogger().Fatalf("unsupport database type:%s", dbType)
	}
	DbClient = initFunc(url)
	return DbClient
}
